Repository navigation
Return fetched rows in row ID order unless the filter orders them - #7
Merged
Merged
Conversation
SQL does not define the order of rows without ORDER BY. The sync reads `id > lastUploadedId` with `LIMIT batchSize` and moves its marker, and deletes rows, up to the last row it read, so it relies on the batch being the lowest IDs in order. SQLite returns them in that order today, but not by contract: with the rows in another order (shown with PRAGMA reverse_unordered_selects), rows that were not uploaded would be skipped and deleted, and others sent twice. The Apple Watch transfer pages the same way. - fetch(filter:limit:) appends ORDER BY rowid (id is the rowid) unless the filter has its own ORDER BY, as in "... ORDER BY timestamp DESC". The query plan is unchanged (rowid range search), and tables without an `id` column still work. - Tests: ID order under reverse_unordered_selects (fails without this change), a filter with its own ORDER BY, and the ORDER BY detection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SQLiteEngine.fetch(filter:limit:)ranSELECT * FROM t WHERE <filter> LIMIT nwith noORDER BY. SQL does not define the row order then.id > lastUploadedIdwithLIMIT batchSize, then moves its marker, and deletes rows, up to the last row read. It relies on the batch being the lowest IDs, in order. SQLite returns them that way today (rowid range search), but not by contract. With another order (reproduced withPRAGMA reverse_unordered_selects), a batch of[10, 9, 8, 7]would upload 7–10, delete 1–7 (1–6 never uploaded), and send 8–10 again.AWDataTransferManager(applewatch) pages the same way.fetchnow appendsORDER BY rowid(idis the rowid) unless the filter has its ownORDER BY, e.g.com.awareframework.ios.context's"timestamp >= … ORDER BY timestamp DESC", which keeps working.Impact
SEARCH t USING INTEGER PRIMARY KEY (rowid>?), no sort step. Measured 0.116 ms vs 0.117 ms for 1,000 rows of a 1M-row table.ORDER BY rowid(rather thanid) also works for tables without anidcolumn.fetch(filter:limit:)callers in aware-client-ios-v3: the sync, applewatch transfer (benefit), context (ownORDER BY, unchanged), aware-client fallback and tests (unchanged).Test plan
swift test: 91 tests passtestFetchReturnsRowsInIdOrderfails without the change ([30, 29, 28]instead of[23, 24, 25]underreverse_unordered_selects) and passes with itORDER BY(upper and lower case) keeps its order;containsOrderBydoes not match words likeborder🤖 Generated with Claude Code